package com.example.administrator.nurse.widget;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.drawable.GradientDrawable;
import android.view.View;

/**
 * Created by Administrator on 2016/4/12.
 */
public class MorphingAnimation {
    private int mDuration;
    private int mToWidth;
    private int mFromWidth;
    private int mToColor;
    private int mFromColor;
    private float mToCornerRadius;
    private float mFromCornerRadius;
    private GradientDrawable mDrawable;
    private View mView;

    public MorphingAnimation(View viewGroup, GradientDrawable drawable){
        this.mView=viewGroup;
        this.mDrawable=drawable;
    }
    public float getmFromCornerRadius() {
        return mFromCornerRadius;
    }

    public float getmToCornerRadius() {
        return mToCornerRadius;
    }

    public GradientDrawable getmDrawable() {
        return mDrawable;
    }

    public int getmDuration() {
        return mDuration;
    }

    public int getmFromColor() {
        return mFromColor;
    }

    public int getmFromWidth() {
        return mFromWidth;
    }

    public int getmToColor() {
        return mToColor;
    }

    public int getmToWidth() {
        return mToWidth;
    }

    public void setmDrawable(GradientDrawable mDrawable) {
        this.mDrawable = mDrawable;
    }

    public void setmDuration(int mDuration) {
        this.mDuration = mDuration;
    }

    public void setmFromColor(int mFromColor) {
        this.mFromColor = mFromColor;
    }

    public void setmFromCornerRadius(float mFromCornerRadius) {
        this.mFromCornerRadius = mFromCornerRadius;
    }

    public void setmFromWidth(int mFromWidth) {
        this.mFromWidth = mFromWidth;
    }

    public void setmToColor(int mToColor) {
        this.mToColor = mToColor;
    }

    public void setmToCornerRadius(float mToCornerRadius) {
        this.mToCornerRadius = mToCornerRadius;
    }

    public void setmToWidth(int mToWidth) {
        this.mToWidth = mToWidth;
    }

    public void setmView(View mView) {
        this.mView = mView;
    }
    public void start(){
        ValueAnimator widthAnimator=ValueAnimator.ofInt(mFromWidth,mToWidth);
        widthAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Integer value = (Integer) animation.getAnimatedValue();
                int leftOffset;
                int rightOffset;
                int padding;
                if (mFromWidth>mToWidth){
                    leftOffset=(mFromWidth-value)/2;
                    rightOffset=mFromWidth-leftOffset;
                    padding= (int) animation.getAnimatedFraction();
                }else {
                    leftOffset=(mToWidth-value)/2;
                    rightOffset=(mToWidth-leftOffset);
                    padding= (int) animation.getAnimatedFraction();
                }
                mDrawable.setBounds(padding,leftOffset+padding,mView.getHeight()-padding,rightOffset-padding);
            }
        });
        ObjectAnimator bgColorAnimator=ObjectAnimator.ofInt(mDrawable,"color",mFromColor,mToColor);
        bgColorAnimator.setEvaluator(new ArgbEvaluator());
        ObjectAnimator cornerAnimator=ObjectAnimator.ofFloat(mDrawable,"cornerRadius",mFromCornerRadius,mToCornerRadius);
        AnimatorSet animatorSet=new AnimatorSet();
        animatorSet.setDuration(mDuration);
        animatorSet.playTogether(widthAnimator, bgColorAnimator, cornerAnimator);
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {

            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animatorSet.start();
    }
}
